import {GET_BRAND_LIST,GET_MAKE_LIST} from " ../actionType";

const initstate={
    brandList:[],
    makeList:[]
}

const cars = (state=initstate,action)=>{
    let newstate = JSON.parse(JSON.stringify(state));
    switch (action.type) {
        case GET_BRAND_LIST:{
            let arr = new Set();
            action.data.forEach((v)=>{
                 arr.add(v.Spelling[0])
            })
            newstate.brandList = Array.from(arr).map((v,i)=>{
                  let children=[];
                  action.data.map(item=>{
                      if(item.Spelling[0]===v){
                          children.push(item)
                      }
                  })

                  return {
                       id:i,
                       FirstSpell:v,
                       children
                  }
            })
        }
            break;
         case GET_MAKE_LIST:newstate.makeList = action.data
         break;
        default:
            break;
    }
    return newstate
}

export default cars